﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public partial class FSMController
{
    protected List<FSMState> m_AllStates;
    public List<FSMState> allStates => m_AllStates;

    protected FSMState m_CurState;
    public FSMState curState => m_CurState;

    protected FSMState defaultState;

    /// <summary>
    /// 无信号
    /// </summary>
    public const int SignNone = -1;

    protected int m_Sign;
    public int sign => m_Sign;

    public FSMController()
    {

    }


    public void SetDefaultState(FSMState state)
    {
        defaultState = state;
    }

    public void AddState(FSMState fSMState)
    {
        if (m_AllStates == null)
        {
            m_AllStates = new List<FSMState>();
        }
        m_AllStates.Add(fSMState);
    }

    public void InputSign(int sign, string logName = "")
    {
        this.m_Sign = sign;
#if UNITY_EDITOR
        GameDebug.Log($"{logName} InputSign:{sign}");
#endif
    }

    public void ResetSign()
    {
        m_Sign = SignNone;
    }

    public void Update(float deltaTime)
    {
        _Update(deltaTime);
        _LateUpdate(deltaTime);
    }
    protected virtual void _Update(float deltaTime)
    {
        if (m_CurState == null)
        {
            if (m_AllStates != null && m_AllStates.Count > 0)
            {
                FSMState firstState = defaultState == null ? m_AllStates[0] : defaultState;
#if UNITY_EDITOR
                GameDebug.Log($"初始状态是：{firstState.GetType().Name}");
#endif

                SetCurState(firstState);
            }
        }
        else
        {
            m_CurState.Update(deltaTime);

            var meetTransition = m_CurState.CheckSign(m_Sign);

            if (meetTransition != null)
            {
                FSMState taregtState = meetTransition.toState;
                if(m_CurState != taregtState)
                    SetCurState(taregtState);

                m_Sign = SignNone;
            }
            else
            {
                meetTransition = m_CurState.CheckTransition();
                if (meetTransition != null)
                {
                    var taregtState = meetTransition.toState;
                    if(m_CurState != taregtState)
                        SetCurState(taregtState);
                }
            }

        }      
        
    }

    protected virtual void _LateUpdate(float deltaTime)
    {
    }
    
    public void SetCurState(FSMState targetState)
    {
        if (null == targetState)
        {
            GameDebug.LogError("DoTransition targetState == null ");
            return;
        }

        if (m_AllStates.Contains(targetState))
        {
            FSMState preState = m_CurState;

            preState?.OnExit(targetState);
            
            //GameDebug.Log($"set state:{targetState.GetEntityTypeID()}  name:{targetState.GetTypeStr()}");

            m_CurState = targetState;

            targetState.OnEnter(preState);
            
            OnStateChange(preState,targetState);
        }
        else
        {
            GameDebug.LogError($"cannot find state:{targetState.stateId}");
        }
    }

    protected virtual void OnStateChange(FSMState preState,FSMState curAction)
    {
        
    }

    public  void Delete()
    {
        m_AllStates = null;
        m_CurState = null;
        defaultState = null;
    }


}
